﻿using RollCall.Data;
using RollCall.Pages;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Navigation;

namespace RollCall.ViewModel
{
    public class ResultViewModel : BaseViewModel
    {
        private readonly NavigationService _navigationService;
        public ICommand ReturnCommand { get; set; }
        public ICommand RandmonCommand { get; set; }
        public string Type { get => _currentGroup.Subject.ToString() + _currentGroup.Name; }

        private readonly Data.Group _currentGroup;

        /// <summary>
        /// 当前学生名称
        /// </summary>
        public string CurrentStudentName
        {
            get => _currentStudent?.Name;

        }

        private Student _currentStudent;


        private readonly List<Student> _students;
        private readonly Random _rnd = new Random();
        public ResultViewModel(NavigationService navigationService, Guid groupId)
        {
            _navigationService = navigationService;
            ReturnCommand = new RelayCommand(s =>
              {
                  _navigationService.GoBack();
              }, s => true);
            _currentGroup = App.MyDbContext.Groups.Find(groupId);
            _students = App.MyDbContext.Students.Where(w => w.GroupId == groupId).ToList();

            RandmonCommand = new RelayCommand(RandmonStudent, s => true);

            RandmonStudent();

        }

        /// <summary>
        /// 随机一个学生
        /// </summary>
        private void RandmonStudent(object obj = null)
        {
            if (_students.Count == 0) return;
            var index = _rnd.Next(0, _students.Count);
            _currentStudent = _students[index];
            OnPropertyChanged(nameof(CurrentStudentName));
        }
    }
}
